package com.qch.kafka.demo.consumer;

import com.qch.kafka.demo.entity.User;
import com.qch.kafka.demo.serialization.UserDeserializer;
import com.qch.kafka.demo.serialization.Userserializer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @Author qch
 * @Date 2021/5/31
 */
public class UserConsumer {
    public static void main(String[] args) {
        Map<String, Object> configs = new HashMap<String, Object>();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.142.135:9092");
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,UserDeserializer.class);
        configs.put(ConsumerConfig.GROUP_ID_CONFIG,"user_consumer");
        configs.put(ConsumerConfig.CLIENT_ID_CONFIG,"consumer_id");
        configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        KafkaConsumer<String,User> consumer = new KafkaConsumer<String,User>(configs);
        consumer.subscribe(Collections.singleton("tp_user_01"));
        final ConsumerRecords<String, User> records = consumer.poll(Long.MAX_VALUE);
        records.forEach(new Consumer<ConsumerRecord<String, User>>() {
            public void accept(ConsumerRecord<String, User> record) {
                System.out.println(record.value());
            }
        });
        consumer.close();
    }
}
